package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_blood_pressure.bp_history

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bp.hearthealth.pulse.lnapps.R
import com.bp.hearthealth.pulse.lnapps.databinding.BloodPressureItemLayoutBgBinding
import com.bp.hearthealth.pulse.lnapps.bp_db.bp_bloodpressure.BloodPressure
import com.bp.hearthealth.pulse.lnapps.bp_db.bp_bloodpressure.BloodPressureData
import com.bp.hearthealth.pulse.lnapps.bp_extension.UIHelperBp
import com.bp.hearthealth.pulse.lnapps.bp_utils.DoubleClickListener

class BpBloodPressureHistoryListAdapter :
    RecyclerView.Adapter<BpBloodPressureHistoryListAdapter.ItemVH>() {
    private var bDList: MutableList<BloodPressure> = mutableListOf()
    private lateinit var onCliclLisener: OnItemClickListener
    private var dataList: MutableList<BloodPressureData> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemVH {
        return ItemVH(
            BloodPressureItemLayoutBgBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    fun setListData(list: List<BloodPressure>, chooseOne: Boolean) {
        bDList.clear()
        this.bDList.addAll(list)
        dataList.clear()
        if (chooseOne) {
            //某一天的数据
            if (list.isNotEmpty()) {
                val todayList = list.first().bloodPressureList
                dataList = todayList.toMutableList()
            }
        } else {
            //多天的数据，取每个bloodPressureList中的第一条
            for (index in list.indices) {
                if (list[index].bloodPressureList.isNotEmpty()) {
                    val moreFirstData = list[index].bloodPressureList.first()
                    dataList.add(moreFirstData)
                }
            }
        }
        notifyDataSetChanged()
    }

    inner class ItemVH(val binding: BloodPressureItemLayoutBgBinding) :
        RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("SetTextI18n")
        fun bindData(item: BloodPressureData) {
            binding.bean = item
            binding.tvPulse.text = "${UIHelperBp.getString(R.string._pulse_str)}: ${item.pulse} ${
                UIHelperBp.getString(
                    R.string._bpm_str
                )
            }"
        }
    }

    override fun onBindViewHolder(holder: ItemVH, @SuppressLint("RecyclerView") position: Int) {
        holder.bindData(dataList[position])
        holder.itemView.setOnClickListener(object : DoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                onCliclLisener.onItemClick(dataList[position].id, dataList[position].date)
            }
        })
    }

    fun setOnItemClickLisener(clickListener: OnItemClickListener) {
        this.onCliclLisener = clickListener
    }

    interface OnItemClickListener {
        fun onItemClick(bpdId: String, date: Long)
    }

    override fun getItemCount(): Int {
        return dataList!!.size
    }
}